const Storage = require('../../utils/storage');
Page({
  data: {
    historyList: []
  },
  onShow() {
    // 云端同步历史（如有登录/云开发环境）
    if (wx.cloud) {
      const db = wx.cloud.database();
      db.collection('rooms').get().then(res => {
        const list = res.data.map(item => ({
          date: item.date || '',
          playerA: item.playerA?.nickName || '',
          playerB: item.playerB?.nickName || '',
          scoreA: item.playerA?.score || 0,
          scoreB: item.playerB?.score || 0,
          winner: item.winner || '',
          mode: item.mode || ''
        }));
        this.setData({ historyList: list });
      });
    } else {
      const list = Storage.get('historyList', []);
      this.setData({ historyList: list });
    }
  },
  onViewDetail(e) {
    const idx = e.currentTarget.dataset.index;
    wx.navigateTo({ url: `/pages/result/index?idx=${idx}` });
  }
});
